package com.sangmo.fts.api.inventory;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.data.json.meta.ExtView;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.inventory.interfaces.IfWaybillCreation;
import com.sangmo.fts.process.inventory.interfaces.IfwaybillReceiveRequest;
import com.sangmo.fts.process.inventory.model.IfContract;
import com.sangmo.fts.process.inventory.model.IfWaybill;
import com.sangmo.fts.process.inventory.model.IfWaybillItem;
import com.sangmo.fts.process.inventory.service.IfWaybillService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/ifWaybill/v1")
@Access("ifWaybill")
public class IfWaybillApi {

    @Autowired
    private IfWaybillService ifWaybillService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<IfWaybill> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid IfWaybillCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(ifWaybillService.create(obj));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    @ResponseBody
    public  ApiResponse remove(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifWaybillService.deleteById(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    @ResponseBody
    public  ApiResponse<PageResult<IfWaybill>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("IfWaybill.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifWaybillService.search(ps));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/read")
    @ResponseBody
    public  ApiResponse<IfWaybill> read(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ExtViewHelper.setGroup("IfWaybill.detail", 2);
        return ApiResponse.make(ifWaybillService.find(id));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/items/list")
    @ResponseBody
    public  ApiResponse<PageResult<IfWaybillItem>> listItemsByPage(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long waybillId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(ifWaybillService.listItemsByPage(waybillId, ps));
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_WAYBILL_SELLER_SHIP_4)
    @Access(":ship")
    @PostMapping("/{trainingId}/{id}/ship")
    @ResponseBody
    public  ApiResponse ship(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifWaybillService.ship(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_WAYBILL_STORAGE_RECEIVE_5)
    @Access(":receive")
    @PostMapping("/{trainingId}/{id}/receive")
    @ResponseBody
    public  ApiResponse receive(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid IfwaybillReceiveRequest request) {
        ifWaybillService.receive(id, request);
        return ApiResponse.make();
    }

    @Access(":selection")
    @PostMapping({"/{trainingId}/selection"})
    @ResponseBody
    public ApiResponse<PageResult<IfWaybill>> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("IfWaybill.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifWaybillService.selection(ps));
    }
}
